package com.jike.shop.cloud.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.jike.shop.cloud.*;
import com.jike.shop.cloud.bo.ActBo;
import com.jike.shop.cloud.bo.ActListRequest;
import com.jike.shop.cloud.bo.OrderBo;
import com.jike.shop.cloud.enums.MallActStatus;
import com.jike.shop.cloud.mapper.MallActGoodsMapper;
import com.jike.shop.cloud.mapper.MallActMapper;
import com.jike.shop.cloud.model.MallActDO;
import com.jike.shop.cloud.model.MallActGoodsDO;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@DubboService(group = "jike-shop",version = "1.0.0",protocol ="dubbo")
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private MallActGoodsMapper mallActGoodsMapper;

    @Autowired
    private MallActMapper mallActMapper;



    //促销服务的Dubbo测试接口
    @Override
    public String test(Long id) {
        return "success";
    }

    @Override
    public List<ActBo> actList(ActListRequest request) {
        List<ActBo> result = new ArrayList<>();
        List<MallActGoodsDO> mallActGoodsDOS = mallActGoodsMapper.selectList(
                new LambdaQueryWrapper<MallActGoodsDO>()
                        .eq(MallActGoodsDO::getGoodsId, request.getGoodsId())
                        .eq(MallActGoodsDO::getGoodsStatus, 1));
        if(CollectionUtils.isEmpty(mallActGoodsDOS)) {
            return result;
        }

        List<String> ids = mallActGoodsDOS.stream().map(MallActGoodsDO::getActId).collect(Collectors.toList());
        List<MallActDO> mallActDOS = mallActMapper.selectList(new LambdaQueryWrapper<MallActDO>()
                .in(MallActDO::getId, ids)
                .eq(MallActDO::getActStatus, MallActStatus.INHAND.getCode())
                .le(MallActDO::getActStartTime, new Date())
                .ge(MallActDO::getActEndTime, new Date()));
        if(CollectionUtils.isEmpty(mallActDOS)) {
            return result;
        }

        result = mallActDOS.stream().map(mallActDO -> {
            ActBo actBo = new ActBo();
            BeanUtils.copyProperties(mallActDO, actBo);
            MallActGoodsDO first = mallActGoodsDOS.stream().filter(mallActGoodsDO -> mallActGoodsDO.getActId().equals(mallActDO.getId())).findFirst().get();
            BigDecimal actualPrice = getActualPrice(request, first, mallActDO.getActRule(), mallActDO);
            actBo.setActPrice(actualPrice);
            return actBo;
        }).collect(Collectors.toList());

        return result;
    }

    public BigDecimal getActualPrice(ActListRequest request,MallActGoodsDO mallActGoodsDO, String rule,MallActDO mallActDO)  {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("goodsDO", mallActGoodsDO);
        context.put("rule", rule);
        context.put("request", request);
        context.put("mallAct",mallActDO);
        try {
            return  (BigDecimal)runner.execute(rule, context, null, true, false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
